package com.lcf.app.druid.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcf.app.druid.entity.User;
import com.lcf.app.druid.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author ChenFei
 * @since 2021-10-26
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    HttpSession session;

    //查询
    @GetMapping("get/{id}")
    public User get(@PathVariable String id) {
        session.setAttribute("session", "session");
        return userService.getById(id);
    }

    //添加
    @PostMapping("save")
    public boolean save() {
        return userService.save(new User()
                .setAge(26)
                .setUserName("ChenFei")
                .setWeight(80.0));
    }

    //编辑
    @PutMapping("update/{id}")
    public boolean update(@PathVariable String id) {
        return userService.updateById(new User()
                .setAge(26)
                .setUserName("ChenFei")
                .setWeight(80.0)
                .setId(id));
    }

    //分页查询
    @GetMapping("list")
    public Page<User> list(Integer pageNum, Integer pageSize) {
        return userService.page(new Page<>(pageNum, pageSize));
    }

    //分页查询-自定义xml
    @GetMapping("page")
    public Page<User> page(Integer pageNum, Integer pageSize) {
        return userService.pageInfo(new Page<>(pageNum, pageSize));
    }

}
